﻿using M.Warehouse.API.Application.Command.InventoryCommand;
using M.Warehouse.Domain;
using M.Warehouse.Domain.DTO;
using M.Warehouse.Repository.WarehouseInterface;
using MediatR;

namespace M.Warehouse.API.Application.CommandHandlers.InventoryHandlers
{
    /// <summary>
    /// 根据产品编号显示产品名称
    /// </summary>
    public class InventoryGetCodeCommandHandler : IRequestHandler<InventoryGetCodeCommand, List<InListDto>>
    {
        /// <summary>
        /// 入库仓储接口
        /// </summary>
        private readonly IInventoryRepository _inventoryRepository;
        /// <summary>
        /// 仓库仓储接口
        /// </summary>
        private readonly IWarehouseWRepository _warehouseWRepository;
        /// <summary>
        /// 库区接口
        /// </summary>
        private readonly IReservoirsWRepository _repository;
        public InventoryGetCodeCommandHandler(IInventoryRepository inventoryRepository, IWarehouseWRepository warehouseWRepository, IReservoirsWRepository repository)
        {
            _inventoryRepository = inventoryRepository;
            _warehouseWRepository = warehouseWRepository;
            _repository = repository;
        }
        public async Task<List<InListDto>> Handle(InventoryGetCodeCommand request, CancellationToken cancellationToken)
        {
            var ss = from a in await _inventoryRepository.GetAllAsync()
                     join b in await _warehouseWRepository.GetAllAsync()
                     on a.WarehouseId equals b.WarehouseId
                     join c in await _repository.GetAllAsync()
                     on a.ReservoirId equals c.ReservoirId
                     where a.FinishedProductNo == request.FinishedProductNo
                     select new InListDto()
                     {
                         FinishedProductNo = a.FinishedProductNo,
                         InventoryNum = a.InventoryNum,
                         LocationCoding = b.LocationCoding,
                         WarehouseName = b.WarehouseName,
                         ReservoirName = c.ReservoirName,
                         CreatedDate=a.CreatedDate,
                     };
            return ss.ToList();
        }
    }
}
